package com.ajk.server.service.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.controller.events.CreditsEvent;
import com.ajk.server.dao.TreatmentMapper;
import com.ajk.server.dao.TreatmentPicMapper;
import com.ajk.server.model.CreditsCategory;
import com.ajk.server.model.Treatment;
import com.ajk.server.model.TreatmentPic;
import com.ajk.server.model.query.TreatmentQuery;
import com.ajk.server.model.request.TreatmentReq;
import com.ajk.server.model.response.TreatmentRes;
import com.ajk.server.service.TreatmentService;
import com.ajk.server.utils.FileStoreUtil;
import com.ajk.server.utils.SpringContextUtil;

/**
 * 就诊记录  <br>
 * t_treatment <br>
 * v1.0 2016-05-01
 */
 @Service
 public class TreatmentServiceImpl implements TreatmentService {
	
	private static final Logger logger = LoggerFactory.getLogger(TreatmentServiceImpl.class);
	
	@Autowired
	private TreatmentMapper treatmentMapper;
	
	@Autowired
	TreatmentPicMapper treatmentPicMapper;
	
	/**
 	*根据ID删除 就诊记录
 	*/
 	@Override
 	public Integer deleteTreatmentById(Integer id){
 		return treatmentMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 就诊记录
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertTreatment(List<? extends Treatment> treatments){
		Integer count = treatmentMapper.batchInsert(treatments);
		return count;
 	}
 	/**
 	*新增 就诊记录
 	*/
 	@Override
 	@Transactional
 	public Integer insertTreatment(Treatment domain){
 		Integer treatmentId = null;
 		TreatmentReq treatment = null;
 		if(domain instanceof TreatmentReq){
 			treatment = (TreatmentReq) domain;
 			treatment.dealWith();
 			
 			treatmentMapper.insert(treatment);
 			
 			treatmentId = treatment.getId();
 			
 			List<TreatmentPic> pics = treatment.getTreatmentPics();
 			
 			if (CollectionUtils.isNotEmpty(pics)) {
				for (TreatmentPic treatmentPic : pics) {
					treatmentPic.setParentId(treatmentId);
				}
				treatmentPicMapper.batchInsert(pics);
			}
 		}
 		
 		//添加积分
 		CreditsEvent event = new CreditsEvent(treatmentId, domain.getUserId(), CreditsCategory.TREAMENT_ADD);
 		SpringContextUtil.getApplicationContext().publishEvent(event);
 		
		return treatmentId;
 	}

 	/**
 	*更新 就诊记录
 	*/
 	@Override
 	@Transactional
 	public Integer updateTreatment(Treatment domain){
 		Integer treatmentId = domain.getId();
 		TreatmentReq treatment = null;
 		if(domain instanceof TreatmentReq){
 			treatment = (TreatmentReq) domain;
 			treatment.dealWith();
 			treatmentMapper.update(treatment);
 			if (ArrayUtils.isNotEmpty(treatment.getDeletePicIds())) {
 				List<Integer> list = Arrays.asList(treatment.getDeletePicIds());
 				treatmentPicMapper.deleteByIds(list);
			}
 			List<TreatmentPic> pics = treatment.getTreatmentPics();
 			if (CollectionUtils.isNotEmpty(pics)) {
				for (TreatmentPic treatmentPic : pics) {
					treatmentPic.setParentId(treatmentId);
				}
				treatmentPicMapper.batchInsert(pics);
			}
 			
 		}
		return treatmentId;
 	}
 	
 	/**
 	*根据ID查询 就诊记录
 	*/
 	public Treatment findTreatmentById(Integer id){
 		Treatment result = treatmentMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		return result;
 	}

	/**
 	*通过query 查询 就诊记录
 	*/
	public List<TreatmentRes> findTreatmentByQuery(TreatmentQuery query){
		query.dealWith();
		List<TreatmentRes> result = treatmentMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			List<Integer> treatmentIds = new ArrayList<Integer>();
			Collections.sort(result);
			Map<Integer, TreatmentRes> map = new HashMap<Integer, TreatmentRes>();	
			for (TreatmentRes treatment : result) {
				treatmentIds.add(treatment.getId());
				map.put(treatment.getId(), treatment);
			}
			if (CollectionUtils.isNotEmpty(treatmentIds)) {
				List<TreatmentPic> pics =  treatmentPicMapper.findByParentIds(treatmentIds);
				if (CollectionUtils.isNotEmpty(pics)) {
					for (TreatmentPic treatmentPic : pics) {
						if(map.containsKey(treatmentPic.getParentId())){
							FileStoreUtil.dealWithTreatmentPic(treatmentPic);
							map.get(treatmentPic.getParentId()).addTreatmentPics(treatmentPic);
						}
					}
				}
			}
		}
		return result;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countTreatmentByQuery(TreatmentQuery query){
 		query.dealWith();
 		Integer result = treatmentMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<Treatment> findTreatmentByIds(List<Integer> ids){
 		List<Treatment> result = treatmentMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (Treatment treatment : result) {
				treatment.dealWith();
			}
		}
		return result;
 	}
 	
}	